<?php

namespace model;

use common\Model;

class feedbacks extends Model
{
    const TABLE = "feedbacks";
	private $DB;
    function __construct()
    {
        $this->DB = parent::db();
    }
    //查找一条数据
    public function findData()
    {
        $where['id'] = $_GET['id'];
        return $this->DB->table(self::TABLE)->where($where)->find();
    }

    //添加回复数据
    public function insertData()
    {
        if (empty($_POST['title']))
            return  ['status' => 0, 'msg' => '请填写必要数据'];
        $data = [
            'title' => VarFilter($_POST['title']),
            'name' => VarFilter($_POST['name']),
            'phone' => VarFilter($_POST['phone']),
            'content' => VarFilter($_POST['content']),
            'ip' => GetIp(),
            'mid' => $_POST['mid'],
            'time' => date('Y-m-d H:i:s', time()),
            'status' => $_POST['status'],
            /* 're_content' => $_POST['re_content'],
			're_status' => $_POST['re_status'],
			're_time' => date('Y-m-d H:i:s',time()), */
        ];
        $re_key = $this->DB->table(self::TABLE)->insert($data);
        if ($re_key) {
            //写入日志
            SqlLog("添加意见反馈 ID：" . $re_key);

            return ['status' => 1, 'msg' => '添加成功'];
        } else {
            return ['status' => 0, 'msg' => '数据错误！添加失败'];
        }
    }
    //更新回复数据
    public function updateData()
    {
        if (empty($_POST['title']))
            return  ['status' => 0, 'msg' => '请填写必要数据'];
        $where['id'] = $_POST['id'];
        $data = [
            'title' => VarFilter($_POST['title']),
            'name' => VarFilter($_POST['name']),
            'phone' => VarFilter($_POST['phone']),
            'content' => VarFilter($_POST['content']),
            //'mid' => $_POST['mid'],
            //'time' => date('Y-m-d H:i:s',time()),
            'status' => $_POST['status'],
            're_content' => VarFilter($_POST['re_content']),
            're_status' => $_POST['re_status'],
            're_time' => date('Y-m-d H:i:s', time()),
        ];
        $re_key = $this->DB->table(self::TABLE)->Where($where)->Update($data);
        if ($re_key > 0) {
            SqlLog("修改或回复意见反馈 ID：" . $_POST['id']);
            return ['status' => 1, 'msg' => '修改成功'];
        } else {
            return ['status' => 0, 'msg' => '数据没有变化'];
        }
    }

    //删除一条数据
    public function deleteOneData()
    {
        $where['id'] = $_POST['id'];
        SqlLog("删除意见反馈信息 ID：" . $_POST['id']);
        return $this->DB->table(self::TABLE)->where($where)->Delete();
    }

    //设置状态
    public function switchStatus()
    {
        $where['id'] = $_POST['id'];
        $data = [
            'status' => $_POST['status']
        ];
        SqlLog("设置意见反馈显示状态 ID：" . $_POST['id']);

        return $this->DB->table(self::TABLE)->where($where)->Update($data);
    }

    //获取数据并分页
    public function jsonPageSelect()
    {
        $p = intval($_GET['page'] ?? 0) ?: 1;
        $num = intval($_GET['limit'] ?? 0) ?: 15;

        $page = ['num' => $num, 'p' => $p, 'return' => true];
        return [
            'data' => $this->DB->table(self::TABLE)->Page($page)->order('id DESC')->select(),
            'page' => $this->DB->getPage()
        ];
    }
}
